package com.situation.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situation.mapper.MenuMapper;
import com.situation.mapper.RoleMapper;
import com.situation.mapper.RoleMenuMapper;
import com.situation.pojo.DO.TbMenu;
import com.situation.pojo.DO.TbRole;
import com.situation.pojo.DO.TbRoleMenu;
import com.situation.pojo.VO.RoleVO;
import com.situation.service.RoleService;
import com.situation.util.RespUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.management.relation.Role;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;


    @Override
    public PageInfo<RoleVO> findRoleList(Integer pageNum, Integer pageSize, RoleVO roleVO) {
        PageHelper.startPage(pageNum, pageSize);
        List<RoleVO> roleList = roleMapper.findRoleList(roleVO);
        return new PageInfo<>(roleList);
    }

    @Override
    public int add(RoleVO roleVO) {
        roleVO.setStatus(true);
        roleVO.setCreateTime(LocalDateTime.now());
        roleVO.setModifiedTime(LocalDateTime.now());
        return roleMapper.add(roleVO);
    }

    @Override
    @Transactional
    public void deleteById(Long id) throws IllegalAccessException {
        RoleVO roleVO = roleMapper.edit(id);
        if (roleVO == null) {
            throw new IllegalAccessException("参数异常");
        }
        roleMapper.delete(id);
        roleMenuMapper.deleteById(id);
    }

    @Override
    public RoleVO edit(Long id) throws IllegalAccessException {
        RoleVO roleVO = roleMapper.edit(id);
        if (roleVO == null)
            throw new IllegalAccessException("参数异常");
        return roleVO;
    }

    @Override
    public int update(Long id, RoleVO roleVO) throws IllegalAccessException {
        RoleVO r = roleMapper.edit(id);
        if (r == null)
            throw new IllegalAccessException("参数异常");
        roleVO.setId(id);
        roleVO.setModifiedTime(LocalDateTime.now());
        return roleMapper.update(roleVO);
    }

    @Override
    public int updateStatus(Long id, Boolean status) throws IllegalAccessException {
        RoleVO edit = roleMapper.edit(id);
        if (edit == null) {
            throw new IllegalAccessException("参数异常");
        }
        int i = 0;
        if (status) {
            i = roleMapper.updateStatus(id, 0);
        } else {
            i = roleMapper.updateStatus(id, 1);
        }
        return i;
    }

    /**
     * 导 excel 用
     *
     * @return
     */
    @Override
    public List<TbRole> findAll() {
        return roleMapper.selectAll();

    }

    @Override
    public List<Long> findMenuIdsByRoleId(Long id) throws IllegalAccessException {
        RoleVO edit = roleMapper.edit(id);
        if (edit == null) {
            throw new IllegalAccessException("参数异常");
        }
        List<Long> ids=new ArrayList<>();

        List<TbRoleMenu> roleMenus = roleMenuMapper.selectByRoleId(id);
        if(!CollectionUtils.isEmpty(roleMenus)){
            for (TbRoleMenu roleMenu : roleMenus) {
                ids.add(roleMenu.getMenuId());
            }
        }
        return ids;

    }

    @Override
    public int authority(Long id, Long[] mids) throws IllegalAccessException {
        RoleVO edit = roleMapper.edit(id);
        if (edit == null) {
            throw new IllegalAccessException("参数异常");
        }
        int r = 0;
        for (Long mid : mids) {
            int i = roleMapper.authority(id, mid);
            r += i;
        }
        return r;
    }
}
